/*
 * Copyright 2008 CSC Consulting Group under the Apache License, 
 * Version 2.0 (the "License"); you may not use this file except in 
 * compliance with the License. You may obtain a copy of the License at 
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable 
 * law or agreed to in writing, software distributed under the License is 
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
 * either express or implied. See the License for the specific language 
 * governing permissions and limitations under the License.
 */
package com.waw.memberMap.plugin.action;

import java.util.ArrayList;
import java.util.Iterator;

import com.jivesoftware.community.*;
import com.jivesoftware.community.action.JiveActionSupport;
import com.waw.memberMap.plugin.PluginUtils;
import com.waw.memberMap.plugin.MemberMapConsts;


/**
 * This action class contains the logic for the Member Map administration page.
 * 
 */
public class MemberMapAdminAction extends JiveActionSupport {
	
	private static final long serialVersionUID = 1L;
	private String gmapKey = "";
	private String addressField = "";
	private String displayField = "";
	private String infoFields = "";
	private String groupField = "";
	
	/** Getter for displayField property */
    public String getDisplayField() {
		return displayField;
	}

    /** Setter for displayField property */
	public void setDisplayField(String displayField) {
		this.displayField = displayField;
	}

	/** Getter for infoFields property.  */
	public String getInfoFields() {
		return infoFields;
	}

	/** Setter for infoFields property. */
	public void setInfoFields(String infoFields) {
		this.infoFields = infoFields;
	}

	/** Setter for the gmapKey property */
	public void setGmapKey(String gmapKey)
	{
		this.gmapKey = gmapKey;
	}
	
   /** Getter for the gmapKey property. */
	public String getGmapKey()
	{
		return gmapKey;
	}
	
	/** Getter for the addressField property. */
	public String  getAddressField()
	{
		return addressField;
	}
	
	/** Setter for the addressField property. */
	public void setAddressField(String  fieldID)
	{
		addressField = fieldID;
	}
	
	/** Getter for the groupField property.  */
	public String getGroupField() {
		return groupField;
	}

	/** Setter for the groupField property. */
	public void setGroupField(String groupField) {
		this.groupField = groupField;
	}
	
	/**
	 * Returns the ProfileFields defined in the system.
	 * @return iterator of ProfileFields.
	 */
	public Iterator<ProfileField> getProfileFields()
	{
		ProfileFieldManager pMan = PluginUtils.getProfileFieldManager();
		return pMan.getProfileFields().iterator();
	}
	
	/**
	 * Returns the TEXT Profile Fields defined in the system.
	 * @return Iterator of TEXT ProfileFields.
	 */
	public Iterator<ProfileField> getTextProfileFields()
	{
		ProfileFieldManager pMan = PluginUtils.getProfileFieldManager();
		ArrayList<ProfileField> retArray = new ArrayList<ProfileField>();
		Iterator<ProfileField> iter = pMan.getProfileFields().iterator();
		while(iter.hasNext())
		{
			ProfileField field = iter.next();
			if(field.getType() == ProfileField.Type.LARGETEXT || field.getType() == ProfileField.Type.TEXT)
				retArray.add(field);
		}
		return retArray.iterator();
	}
	
	/**
	 * Returns the list of BOOLEAN Profile Fields defined in the system.
	 * @return Iterator of BOOLEAN ProfileField
	 */
	public Iterator<ProfileField> getBooleanProfileFields()
	{
		ProfileFieldManager pMan = PluginUtils.getProfileFieldManager();
		ArrayList<ProfileField> retArray = new ArrayList<ProfileField>();
		Iterator<ProfileField> iter = pMan.getProfileFields().iterator();
		while(iter.hasNext())
		{
			ProfileField field = iter.next();
			if(field.getType() == ProfileField.Type.BOOLEAN)
				retArray.add(field);
		}
		return retArray.iterator();
	}
	
	/**
	 * Returns the list of SINGLELIST Profile Fields defined in the system.
	 * @return Iterator of ProfileFields.
	 */
	public Iterator<ProfileField> getListProfileFields()
	{
		ProfileFieldManager pMan = PluginUtils.getProfileFieldManager();
		ArrayList<ProfileField> retArray = new ArrayList<ProfileField>();
		Iterator<ProfileField> iter = pMan.getProfileFields().iterator();
		while(iter.hasNext())
		{
			ProfileField field = iter.next();
			if(field.getType() == ProfileField.Type.SINGLELIST)
				retArray.add(field);
		}
		return retArray.iterator();
	}
	
    /**
     * The execute method sets the Global properties from the setting on the admin pages.
     * Those properties are:
     * <ul>
     * <li><strong>GLOBAL_MMAP_KEY</strong>The Google Map Key.
     * <li><strong>GLOBAL_MMAP_ADDRESS</strong>The address profile field ID used to calculate Google Map location.
     * <li><strong>GLOBAL_MMAP_DISPLAY</strong>The profile field ID for the field that allows the user to control whether their location are shown on Member Maps.
      * <li><strong>GLOBAL_MMAP_INFO</strong>The IDs of the profile fields to show on InfoWindows on Google Maps.
      * <li><strong>GLOBAL_MMAP_GROUP</strong>The ID of the profile field via which the user selects their desired Well At Work groups.
     * 
     * @see com.jivesoftware.community.action.JiveActionSupport#execute()
     */
    public String execute() {
    	
    	if (getGmapKey() != null && !getGmapKey().equals(""))
    		JiveGlobals.setJiveProperty(MemberMapConsts.GLOBAL_MMAP_KEY, getGmapKey());
    	if(getAddressField() != null && !getAddressField().equals(""))
    		JiveGlobals.setJiveProperty(MemberMapConsts.GLOBAL_MMAP_ADDRESS, getAddressField());
    	if(getDisplayField() != null && !getDisplayField().equals(""))
    		JiveGlobals.setJiveProperty(MemberMapConsts.GLOBAL_MMAP_DISPLAY, getDisplayField());
    	if(getInfoFields() != null && !getInfoFields().equals(""))
    		JiveGlobals.setJiveProperty(MemberMapConsts.GLOBAL_MMAP_INFO, getInfoFields());
    	if(getGroupField() != null && !getGroupField().equals(""))
    		JiveGlobals.setJiveProperty(MemberMapConsts.GLOBAL_MMAP_GROUP,getGroupField());
    	
        return SUCCESS;
    }

}
